package com.mdd.admin.service.auth.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundAuthOrderUnfreezeModel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.admin.service.auth.AlipayAuthOrderFreezeService;
import com.mdd.common.entity.OrderFlowInfo;
import com.mdd.common.entity.agreement.AlipayAuthOrderFreeze;
import com.mdd.common.enums.auth.FundAuthFreezeStatus;
import com.mdd.common.enums.order.type.OrderFlowTypeEnum;
import com.mdd.common.mapper.OrderFlowInfoMapper;
import com.mdd.common.mapper.auth.AlipayAuthOrderFreezeMapper;
import com.mdd.common.util.CodeUtils;
import com.mdd.service.auth.PreAuthorizationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Objects;

import static com.mdd.common.enums.auth.FundAuthFreezeStatus.FUND_AUTH_UNFREEZE;

/**
* @author DELL
* @description 针对表【alipay_auth_order_freeze(预授权押金记录)】的数据库操作Service实现
* @createDate 2023-11-01 17:23:05
*/
@Service
public class AlipayAuthOrderFreezeServiceImpl extends ServiceImpl<AlipayAuthOrderFreezeMapper, AlipayAuthOrderFreeze>
    implements AlipayAuthOrderFreezeService {
    @Resource
    AlipayAuthOrderFreezeMapper alipayAuthOrderFreezeMapper;
    @Resource
    PreAuthorizationService preAuthorizationService;
    @Resource
    DefaultAlipayClient defaultAlipayClient;

    @Resource
    OrderFlowInfoMapper orderFlowInfoMapper;
    @Transactional

    @Override
    public void updateAuthOrderStatus(String outOrderNo) {
        //更新授权状态
        LambdaQueryWrapper<AlipayAuthOrderFreeze> alipayAuthOrderFreezeWrapper=new LambdaQueryWrapper<>();
        alipayAuthOrderFreezeWrapper.eq(AlipayAuthOrderFreeze::getOutOrderNo,outOrderNo);
        AlipayAuthOrderFreeze authOrderFreeze = alipayAuthOrderFreezeMapper.selectOne(alipayAuthOrderFreezeWrapper);
        authOrderFreeze.setStatus(FundAuthFreezeStatus.FUND_AUTH_FREEZE_TO_CREATED.getCode());
        alipayAuthOrderFreezeMapper.updateById(authOrderFreeze);
    }

    @Override
    public AlipayAuthOrderFreeze getAuthInfo(String outOrderNo) {
        LambdaQueryWrapper<AlipayAuthOrderFreeze> authOrderFreezeWrapper=new LambdaQueryWrapper<>();
        authOrderFreezeWrapper.eq(AlipayAuthOrderFreeze::getOutOrderNo,outOrderNo);
        return getOne(authOrderFreezeWrapper);
    }

    @Transactional
    @Override
    public void cancelAuthOrderFreeze(Long orderId) throws AlipayApiException {
        AlipayAuthOrderFreeze alipayAuthOrderFreeze = alipayAuthOrderFreezeMapper.selectOne(new LambdaQueryWrapper<AlipayAuthOrderFreeze>().eq(AlipayAuthOrderFreeze::getOrderId,orderId).orderByDesc(AlipayAuthOrderFreeze::getId).last("limit 1"));
        //Assert.notNull(alipayAuthOrderFreeze,"押金冻结记录不存在！");
        if(Objects.isNull(alipayAuthOrderFreeze)){
            return;
        }
        //Assert.isTrue(alipayAuthOrderFreeze.getStatus().equals(FUND_AUTH_FREEZE.getCode()),"当前状态不能进行押金解冻操作！");
        OrderFlowInfo orderFlowInfo = saveFlowInfoLog(orderId, OrderFlowTypeEnum.CANCEL_DEPOSIT_FREEZE,alipayAuthOrderFreeze.getAmount());

        AlipayFundAuthOrderUnfreezeModel alipayFundAuthOrderUnfreezeModel =new AlipayFundAuthOrderUnfreezeModel();
        alipayFundAuthOrderUnfreezeModel.setAuthNo(alipayAuthOrderFreeze.getAuthNo());
        alipayFundAuthOrderUnfreezeModel.setAmount(alipayAuthOrderFreeze.getAmount().toString());
        alipayFundAuthOrderUnfreezeModel.setOutRequestNo(orderFlowInfo.getFlowNo());
        alipayFundAuthOrderUnfreezeModel.setRemark("解冻押金");
        //取消授权
        preAuthorizationService.unfreeze(defaultAlipayClient,alipayFundAuthOrderUnfreezeModel);
        alipayAuthOrderFreeze.setStatus(FUND_AUTH_UNFREEZE.getCode());
        alipayAuthOrderFreezeMapper.updateById(alipayAuthOrderFreeze);
    }

    /**
     * 流水日志
     * @param orderId
     * @param orderFlowTypeEnum
     * @param amount
     * @return
     */
    @Transactional
    public OrderFlowInfo saveFlowInfoLog(Long orderId , OrderFlowTypeEnum orderFlowTypeEnum, BigDecimal amount){
        OrderFlowInfo orderFlowInfo =   new OrderFlowInfo();
        orderFlowInfo.setFlowNo(CodeUtils.getFlowNo(orderFlowTypeEnum));
        orderFlowInfo.setAmount(amount);
        orderFlowInfo.setOrderid(orderId);
        orderFlowInfo.setType(orderFlowTypeEnum.getCode());
        orderFlowInfo.setItemName(orderFlowTypeEnum.getMsg());
        orderFlowInfo.setSourceAppId(0);
        orderFlowInfoMapper.insert(orderFlowInfo);
        return orderFlowInfo;
    }
}




